﻿using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using OrderCreator.NHibernate.DataModel.Domain;
using OrderCreator.NHibernate.DataModel.Repositories;

namespace OrderCreator.Tests
{
    public class UnitTestBase
    {
        private Configuration _configuration;

        private Product[] _products;
        public Product[] Products
        {
            get { return _products; }
        }

        private Category[] _categories;
        public Category[] Categories
        {
            get { return _categories; }
        }

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            _configuration = new Configuration();
            _configuration.Configure();
            _configuration.AddAssembly(typeof(Product).Assembly);
        }

        [SetUp]
        public void SetupContext()
        {
            new SchemaExport(_configuration).Execute(false, true, false);
            CreateInitialData();
        }

        private void CreateInitialData()
        {
            IProductRepository productRepository = new ProductRepository();
            ICategoryRepository categoryRepository = new CategoryRepository();

            Category categoryBier = new Category { Name = "Bier", TaxPercentage = 19 };
            Category categorySterkeDrank = new Category { Name = "Sterke drank", TaxPercentage = 19 };
            _categories = new[] { categoryBier, categorySterkeDrank };

            foreach (Category category in _categories)
            {
                categoryRepository.Add(category);
            }

            _products = new[]
                        {
                            new Product {Name = "Bacardi Regular", Price = 17.50, Quantity = 12, Category = categorySterkeDrank},
                            new Product {Name = "Bacardi Lemon", Price = 18.50, Quantity = 10, Category = categorySterkeDrank},
                            new Product {Name = "Bacardi Razz", Price = 18.99, Quantity = 6, Category = categorySterkeDrank},
                            new Product {Name = "Amstal bier fust 30 liter", Price = 88.50, Quantity = 5, Category = categoryBier},
                            new Product {Name = "Amstal bier fust 50 liter", Price = 108.50, Quantity = 2, Category = categoryBier},
                        };
            foreach (Product product in _products)
            {
                productRepository.Add(product);
            }
        }
    }
}
